<?php

namespace app\admin\dao;

use app\common\model\AccessLog;
use ShidianUtil\Thinkphp\Dao\DaoTrait;
use app\common\model\TempToken;

/**
 * TempTokenDao 数据访问对象
 * Class TempTokenDao
 * @package app\admin\dao
 * @method TempToken findByPK(?int $id, $field = true)
 * @method \think\Collection selectAll(?int $page, ?int $page_size, $field = true)
 * @method TempToken insertActive(array $params, array $allow_field = [])
 * @method TempToken updateActive(TempToken $model, array $params, array $allow_field = [])
 * @method TempToken findAndUpdateActive(int $id, array $params, array $allow_field = [])
 * @method TempToken updateActiveByClosure(\Closure $closure, array $params, array $allow_field = [])
 * @method void deleteModel(TempToken $model)
 * @method void deleteById(int|array|\Closure $id, bool $force = false)
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037579
 * @link https://www.kancloud.cn/manual/think-orm/1257998
 * @link https://www.kancloud.cn/thinkphp/master-database-and-model/265551
 */
class TempTokenDao
{
    use DaoTrait;

    /**
     * @var TempToken
     */
    private $baseModel;

    /**
     * TempTokenDao constructor.
     */
    public function __construct()
    {
        $this->baseModel = TempToken::class;
    }

    /**
     * 待售案源列表
     * @param $startTime
     * @param $endTime
     * @param $kw
     * @param int|null $page
     * @param int|null $page_size
     * @return array
     */
    public function listLawCases($startTime, $endTime, $kw, ?int $page, ?int $page_size): array
    {
        $chains = ['h5-lawyer'];

        $tableName = TempToken::getTable();

        $query = TempToken::leftJoin('ls_member m', "$tableName.phone=m.phone and m.status in (0, 1)")
            ->whereIn("$tableName.chain", $chains)
            ->when(is_numeric($startTime), fn($query) => $query->where("$tableName.created", '>=', $startTime))
            ->when(is_numeric($endTime), fn($query) => $query->where("$tableName.created", '<=', $endTime))
            ->search("$tableName.phone", $kw);

        $query->tableField(true, $tableName)
            ->with([
                'member' => fn($query) => $query->shortMember()->field(['isSettled']),
            ])
            ->order("$tableName.id", 'DESC')
            ->paging($page, $page_size);

        return listOutput($query, $page_size);
    }

}
